/********************************************************************************
 * LetterServiceDelegator.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Aug 29, 2008 9:58:50 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;
import static com.printeverywhere.util.LoggerConstants.LETTERSLISTBYSUBJECT;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.Letters;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class LetterServiceDelegator.
 */
public class LetterServiceDelegator {

	
	private static Logger log = Logger.getLogger(LetterServiceDelegator.class);
	
	/**
	 * Gets the list.
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Letters> getList() throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			List<Letters> list = session.createCriteria(Letters.class).list();
			transaction.commit();
			return list;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Gets the letter.
	 * 
	 * @param letterId the letter id
	 * 
	 * @return the letter
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static Letters getLetter(int letterId) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(Letters.class);
			criteria.add(Restrictions.eq("letterId", letterId));
			List<Letters> letterList = criteria.list();
			transaction.commit();
			return (letterList.size() > 0) ? letterList.get(0) : new Letters();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}
	
	public static List<Letters> getLettersBySubject(String Subject) throws HibernateException {
		if (log.isInfoEnabled()) {
			log.info(LETTERSLISTBYSUBJECT);
		}

		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Letters.class);
		criteria.add(Restrictions.eq(AttributeConstants.SUBJECT, Subject));
		Transaction transaction = session.beginTransaction();
		List<Letters> lettersList;
		try {
			lettersList = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("HibernateException " + e);
			throw e;
		} finally {
			if (log.isInfoEnabled()) {
				log.info(CLOSING_SESSION);
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return (lettersList.size() == 0) ? null : lettersList;
	}

	/**
	 * Insert letter.
	 * 
	 * @param letter the letter
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void insertLetter(Letters letter) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			session.save(letter);
			transaction.commit();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Update letter.
	 * 
	 * @param letter the letter
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateLetter(Letters letter) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			session.update(letter);
			transaction.commit();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Delete letter.
	 * 
	 * @param letter the letter
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteLetter(Letters letter) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			session.delete(letter);
			transaction.commit();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}
}